/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package iamutkarshtiwari.github.io.ananas.editimage.adapter;

import iamutkarshtiwari.github.io.ananas.ResourceTable;
import iamutkarshtiwari.github.io.ananas.editimage.fragment.crop.CropFragment;
import ohos.agp.components.*;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.util.List;

/**
 * 裁剪框，adapter
 */
public class CropProvider extends BaseItemProvider {

    private Context context;
    private List<String> itemList;
    private int lastPoisition = 0;
    private static int SELECTED_COLOR = ResourceTable.Color_white;
    private static int UNSELECTED_COLOR = ResourceTable.Color_text_color_gray_3;
    private CropFragment fragment;
    private Text selectText = null;
    private Text zeroText = null;

    /**
     * 构造
     *
     * @param context   Context对象
     * @param container ComponentContainer对象
     * @param itemList  item列表
     */
    public CropProvider(Context context, ComponentContainer container, List<String> itemList) {
        this.context = context;
        this.fragment = (CropFragment) container;
        this.itemList = itemList;
    }

    @Override
    public int getCount() {
        return itemList == null ? 0 : itemList.size();
    }

    @Override
    public Object getItem(int position) {
        if (itemList != null && position >= 0 && position < itemList.size()) {
            return itemList.get(position);
        }

        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        if (component == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_crop_item, null, false);
        } else {
            cpt = component;
        }

        Text text = (Text) cpt.findComponentById(ResourceTable.Id_crop_item);
        text.setText(itemList.get(position));
        toggleButtonStatus(text, position == lastPoisition);
        if (position == 0) {
            zeroText = text;
        }
        if (position == lastPoisition) {
            selectText = text;
        }

        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                fragment.clickedCropItem(position);
                selectText.setTextColor(getColorFromRes(UNSELECTED_COLOR));
                selectText.setFont(Font.DEFAULT_BOLD);
                text.setTextColor(getColorFromRes(SELECTED_COLOR));
                text.setFont(Font.DEFAULT_BOLD);
                selectText = text;
                lastPoisition = position;
            }
        });
        return cpt;
    }

    public void setLastPoisition(int position) {
        lastPoisition = position;
    }

    public void reset() {
        selectText.setTextColor(getColorFromRes(UNSELECTED_COLOR));
        selectText.setFont(Font.DEFAULT_BOLD);
        if (zeroText != null) {
            zeroText.setTextColor(getColorFromRes(SELECTED_COLOR));
            zeroText.setFont(Font.DEFAULT_BOLD);
        }
    }

    private void toggleButtonStatus(Text view, boolean isActive) {
        view.setTextColor(getColorFromRes((isActive) ? SELECTED_COLOR : UNSELECTED_COLOR));
        view.setFont(Font.DEFAULT_BOLD);
    }

    private Color getColorFromRes(int resId) {
        Color color = Color.GRAY;
        try {
            color = new Color(context.getResourceManager().getElement(resId).getColor());
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
        return color;
    }
}
